var nameAvail=false;
function register()
{
	isAvailName();
	if(document.regform.passw.value!=document.regform.passw2.value)
		alert("Your passwords do not match");	
	else if(document.regform.fname.value.length<1){	
		alert("You must enter your First Name"); 
		document.regform.fname.focus();
	}else if(document.regform.lname.value.length<1){	
		alert("You must enter your Last Name"); 
		document.regform.lname.focus();
	}else if(checkpassw()&& checkemail()&& checkphno() && checkcrno()){
		if(nameAvail){
			if(agree){
				showmessage("register.jsp"+getSubmitQuery(document.regform),"databar");
			}else{
				alert("You must agree to our Terms and Policy");
			}
			
		}
	}
}
function checkname(){
	if(document.regform.uname.value.length<6){
		document.getElementById("m_uname").innerHTML="Name must be atleast 6 characters long";
		document.getElementById("m_uname").style.color="red" ;
		document.regform.uname.focus();
		return false;
	}else{
		document.getElementById("m_uname").innerHTML="";
		return true;
		}
}
function firstname(){
	if(document.regform.fname.value.length<6){
		document.getElementById("m_fname").innerHTML=" First Name must be atleast 6 characters long";
		document.getElementById("m_fname").style.color="red" ;
		document.regform.uname.focus();
		return false;
	}else{
		document.getElementById("m_fname").innerHTML="";
		return true;
		}
}
function lastname(){
	if(document.regform.lname.value.length<6){
		document.getElementById("m_lname").innerHTML=" Last Name must be atleast 6 characters long";
		document.getElementById("m_lname").style.color="red" ;
		document.regform.uname.focus();
		return false;
	}else{
		document.getElementById("m_lname").innerHTML="Name OK";
		document.getElementById("m_lname").style.color="green" ;
		return true;
		}
}
function checkpassw(){
	var len=document.regform.passw.value.length; 
	if(len<5){
		document.getElementById("m_passw").innerHTML="Too Short (Min 5 chars)";
		document.getElementById("m_passw").style.color="red" ;
		document.regform.passw.focus();
		return false;
	}else if(len <8){
		document.getElementById("m_passw").innerHTML="Weak";
		document.getElementById("m_passw").style.color="Blue";
		return true ;
	}else{
		document.getElementById("m_passw").innerHTML="Strong" ;
		document.getElementById("m_passw").style.color="Green";
		return true ;
	}
}
function checkemail(){
	var str=document.regform.email.value;
	var len=str.length;
	var iat=str.indexOf("@");
	var idot2=str.indexOf(".",iat);
	if(iat<1||(iat>idot2)|| idot2>=len-1){
		document.getElementById("m_email").innerHTML="Enter valid email";
		document.getElementById("m_email").style.color="red" ;
		document.regform.email.focus();
		return false;
	}else{
		document.getElementById("m_email").innerHTML="";
		return true;
	}
}
function checkLuhn(crno){
     var sum = 0;
     var i;
     var nDigits = crno.length;
     var parity = nDigits % 2;
     for(i=0;i<nDigits;i++){
         var digit=crno.charAt(i); 
         if (i%2==parity)
             digit = digit * 2;
         if (digit > 9)
             digit=digit%10+parseInt(digit/10);  
         sum = sum + digit;
     }     
     if(sum%10==0)
     	return true;
     else
     	return false;
 }
 
function checkcrno(){
	var crno=document.regform.crno.value;
	var valid=false;
	var cardtype;
	var len=crno.length;
	if(len>12&&len<17){
		switch(crno.charAt(0)){
			case "5": //Master Card
				var c2=crno.charAt(1);
				if(len==16 && c2<=5 && c2>=1 && checkLuhn(crno)){				
					valid=true;
					cardtype="Master Card";
				}
				break;
			case "4": //VISA
				if((len==16 || len==14)&& checkLuhn(crno)){				
					valid=true;
					cardtype="VISA";
				}
		}
	}
	if(valid){
		document.getElementById("m_crno").innerHTML=cardtype;
		document.getElementById("m_crno").style.color="green" ;						
	}else{
		document.getElementById("m_crno").innerHTML="Enter valid Credit Card No.";
		document.getElementById("m_crno").style.color="red" ;
		document.regform.crno.focus();
	}		
	return valid
}
function checkphno(){	
	var len=document.regform.phno.value.length;
		if(len==0){
			document.getElementById("m_phno").innerHTML="";
			return true;
		}
		else if(isNumber(document.regform.phno.value)){
			if(len>9&&len<13){
				document.getElementById("m_phno").innerHTML="";
				return true;
			}else{
				document.getElementById("m_phno").innerHTML="Phone No should only contain 10 to 12 digits or leave empty";
				document.getElementById("m_phno").style.color="red" ;			
			}
		}else{
			document.getElementById("m_phno").innerHTML="Phone No should contain only digits";
			document.getElementById("m_phno").style.color="red" ;
			}	
	return false;
}
function isNumber(char){
	var numcheck = /\D/;
	var i;
	for(i=0;i<char.length;i++){
		if(numcheck.test(char.charAt(i)))
			return false;
	}	
	return true;
}

function isAvailName(){
	if(checkname()){
		xmlHttp.open("POST","checkname.jsp?name="+document.regform.uname.value,false);
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				if(xmlHttp.responseText.indexOf("false")!=-1){
					document.getElementById("m_uname").innerHTML="This User Name is already in use.";
					document.getElementById("m_uname").style.color="red" ;
					nameAvail=false;
				}else{
					document.getElementById("m_uname").innerHTML="Hurray! User name is available.";
					document.getElementById("m_uname").style.color="Green" ;
					nameAvail=true;
				}
			}
		}
		xmlHttp.send(null);
	}
}
function comparepassw(){
	var len=document.regform.passw.value.length;
	var len1=document.regform.passw2.value.length;
	var val=document.regform.passw.value;
	var val1=document.regform.passw2.value;
	
	if((len != len1) || (val != val1)){
		document.getElementById("m_passw2").innerHTML="Password Doesnt Match";
		document.getElementById("m_passw2").style.color="red" ;
		document.regform.passw2.focus();
		return false;
	}else {
		document.getElementById("m_passw2").innerHTML="";
		return true; ;
	}
}

function submit(){
	var val=document.regform.passw.value;
	var val1=document.regform.passw2.value;
	var val2=document.regform.email.value;
	var val3=document.regform.uname.value;
	var val4=document.regform.phno.value;	
	if(val3 == ""){
		document.getElementById("m_uname").innerHTML="UserName Cannot Be Empty";
		document.getElementById("m_uname").style.color="red" ;
		document.regform.uname.focus();
		return false;
	}else if(val2 == "") {
			document.getElementById("m_email").innerHTML="Email Cannot Be Empty";
			document.getElementById("m_email").style.color="red" ;
			document.regform.email.focus();
			return false;
	}
	else if(val == ""){
		document.getElementById("m_passw").innerHTML="Please Enter The Password";
		document.getElementById("m_passw").style.color="red" ;
		document.regform.passw.focus();
		return false;
	}
	else if(val1 == ""){
		document.getElementById("m_passw2").innerHTML="Retype the password heres";
		document.getElementById("m_passw2").style.color="red" ;
		document.regform.passw2.focus();
		return false;
	}
	else if(val4 == ""){
		document.getElementById("m_phno").innerHTML="Please Ente the Phone Number ";
		document.getElementById("m_phno").style.color="red" ;
		document.regform.phno.focus();
		return false;
	}
	else {
		document.getElementById("m_uname").innerHTML="";
		document.getElementById("m_email").innerHTML="";
		document.getElementById("m_passw").innerHTML="";
		document.getElementById("m_passw2").innerHTML="";
		document.getElementById("m_phno").innerHTML="";
		document.getElementById("m_submit").innerHTML="Password Sent to Ur Mail...";
		return true;
		
	}
}
function passsubmit(){
	
	var str=document.regform.email.value;
	if(str == ""){
		document.getElementById("m_email").innerHTML="Enter valid email";
		document.getElementById("m_email").style.color="red" ;
		document.regform.email.focus();
		return false;
	}else{
		document.getElementById("m_submit").innerHTML=" Password Successfully sent to mail. Check Out";
		document.getElementById("m_email").innerHTML="";
		document.regform.email.focus();
		return true;
	}

}